#include "clock-arch.h"
#include "LPC17xx.h"
#include "lpc17xx_gpio.h"
__IO clock_time_t Ticks;

typedef struct
{
  char port;
  char pin;
} t_led;

extern t_led led[];

#ifdef __KEIL_17xx__
#define BLINK_LED 7
#else
#define BLINK_LED 0
#endif

/* SysTick timer interrupt  handler */
void SysTick_Handler (void)
{
	++Ticks;
	if (!(Ticks & 0x07)){
    unsigned long port = led[BLINK_LED].port;
    unsigned long pin_mask = (1<<led[BLINK_LED].pin);
    if(GPIO_ReadValue(port) & pin_mask)
    {
      GPIO_ClearValue(port,pin_mask);
    }
    else
    {
      GPIO_SetValue(port,pin_mask);
    }
	}
}

/* Timer init */
void clock_init(void)
{
	Ticks = 0;

	// NXP: Initialize System tick timer
	// Generate interrupt each SYSTICK_PERIOD microsecond
	if (SysTick_Config((SystemFrequency/CLOCK_CONF_SECOND))){
		// Capture error
		while (1);
	}
}

/* returned The current clock time, measured in system ticks */
clock_time_t clock_time(void)
{
  return(Ticks);
}
